<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    public function add(){
        $model = $this->getModel();
        $model->load();
        
        parent::display();
    }
    
    public function search_ocba(){
        $model = $this->getModel();
        $filter = JRequest::getVar('filter');
        $ocba_list = $model->searchOcba($filter);
        
        foreach($ocba_list as $key => $row){
            $row_cls = $key % 2;
            echo "
            <tr class='row$row_cls'>
                <td class='left'><a class='ZebraDialog_Button0' href='javascript:void(0)' onclick='select_ocba(\"$row->OCBA_CODE\",\"$row->OCBA_NAME\")' >$row->OCBA_CODE</a> </td>
                <td class='left'>$row->OCBA_NAME</td>
                <td class='left'>$row->OCBA_KANA</td>
            </tr>";
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    public function not_input_ocba(){
        $model = $this->getModel();
        $cc_type = JRequest::getVar('cc_type');
        $date = JRequest::getVar('date');
        $filter = array('cc_type' => $cc_type, 'date' => $date);
        
        foreach($model->list_ocba_not_input($filter) as $key => $row){
            $cls = ($key + 1) % 2;
            echo 
                "<tr class='row$cls'>".                
                    "<td class='left'>$row->OCBA_CODE</td>".
                    "<td class='left'>$row->OCBA_NAME</td>".                
                "</tr>";
        }
        
        $app = &JFactory::getApplication();
        $app->close();
    }

    public function export(){
        $model = $this->getModel();
        $cc_type = JRequest::getVar('cc_type');
        $date = JRequest::getVar('date');
        $filter = array('cc_type' => $cc_type, 'date' => $date);

        $csv_output = array();
        array_push($csv_output, "Code,Name");
        foreach($model->list_ocba_not_input($filter) as $key => $row){
            array_push($csv_output, "'$row->OCBA_CODE',$row->OCBA_NAME");
        }
        
        $csv_output = implode("\r\n", $csv_output);
        $csv_output = mb_convert_encoding($csv_output,'Shift-JIS','UTF-8');
        
        
        $filename = "statistic-ocba-not-input_".date("Y-m-d_H-i",time());

        header("Content-Encoding: Shift-JIS");
        header("Content-Type: text/csv; charset= Shift-JIS");
        header("Content-disposition: csv" . date("Y-m-d") . ".csv");
        header("Content-disposition:attachment; filename=" . $filename . ".csv");
        print_r($csv_output);
        
        $app = &JFactory::getApplication();
        $app->close();
    }
}